package com.burgos.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Statistics extends Activity implements OnClickListener {

	private TextView t;
	JSONParser jsonParser = new JSONParser();
	private ProgressDialog pDialog;
	private static final String READ_STATS = "http://10.0.2.2/remote/getstats.php";
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";
	private static String message;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_statistics);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		new loadStats().execute();
		
	}
	
	@Override
	public void onClick(View v) {
	}
	
	public class loadStats extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Statistics.this);
			pDialog.setMessage("Retrieving Product Info...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... args){
			// get statistics
			int success;
			
			try {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("keyword", "stats"));
				// making HTTP request and getting the response
				// SSL
				JSONObject json = jsonParser.makeHttpRequest(READ_STATS, "POST", params);
				success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
				message = json.getString(TAG_MESSAGE);
				} else {
					return json.getString(TAG_MESSAGE);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			//TODO end dialog
			pDialog.dismiss();
			if (file_url != null) {
				Toast.makeText(Statistics.this, file_url, Toast.LENGTH_LONG).show();
			}
			t=(TextView)findViewById(R.id.statisticshead); 
			t.setText("TOTAL IS: " + message);
			}
	}
}

